<template>
  <div class="container">
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="60%"
      @close="handleCancel"
    >
      <el-form
        label-width="80px"
        :model="ruleForm"
        :rules="rules"
        ref="ruleFormRef"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input
            v-model="ruleForm.title"
            placeholder="请输入文章标签"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <!-- 富文本编辑器组件 -->
          <quill-editor
            style="height: 200px"
            v-model="ruleForm.articleBody"
            :options="editorOption"
            ref="myQuillEditor"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" style="margin-top: 80px">
          <el-input
            v-model="ruleForm.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import { detail } from '../../api/hmmm/articles'
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  [{ list: 'ordered' }, { list: 'bullet' }],
  ['blockquote'],
  ['image', 'link']
]
export default {
  props: {
    dialogVisible: { type: Boolean },
    editDialogVisible: { type: Boolean },
    articleID: { type: [String, Number] },
    articleDetial: { type: Object }
  },
  data () {
    return {
      // 表单绑定对象
      ruleForm: {
        id: '',
        title: '', // 文章标题
        videoURL: '', // 视频地址
        articleBody: '' // 编辑区域内容
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      },
      editorOption: {
        //  富文本编辑器配置
        modules: {
          // 工具栏定义的
          toolbar: toolbarOptions
        },
        // 主题
        theme: 'snow',
        placeholder: '请输入正文'
      }
    }
  },
  computed: {
    title () {
      if (this.ruleForm.id) {
        return '修改文章'
      }
      return '新增文章'
    }
  },
  methods: {
    // 将文章详情数据展示(父调用子)
    // async showArticle (id) {
    //   const { data } = await detail({ id: id })
    //   this.ruleForm = data
    // },

    // 确定按钮
    async btnOk () {
      // 检验表单再去触发修改事件
      await this.$refs.ruleFormRef.validate()
      if (this.ruleForm.id) {
        // 如果有id 触发修改事件
        this.$emit('editArticle', this.ruleForm)
      } else {
        // 否则是 新增
        this.$emit('addArticle', this.ruleForm)
        this.$emit('getArticlesList')
      }
      // 都要 渲染页面 重置表单 清空数据
      this.handleCancel()
    },
    // 取消
    handleCancel () {
      if (this.ruleForm.id) {
        this.$refs.ruleFormRef.resetFields()
      } else {
        this.ruleForm = {}
        this.$refs.ruleFormRef.resetFields()
      }
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>

<style scoped lang="less"></style>
